package com.engine;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel implements Runnable
{
    private String m_ImageFile;             // the path and name of the image file to be displayed
    private BufferedImage m_Image;          // instance of the image to be displayed
    private BufferedImage m_ScaledImage;    // scaled instance of the image to be displayed
    private boolean start=true;
    
    /** Creates a new instance of ImagePanel */
    public ImagePanel() {
    	String[] strColor = ReadConfig.GET("IMAGE_COLOR").split("\\,");
	    Color col = new Color(Integer.parseInt(strColor[0]),Integer.parseInt(strColor[1]),Integer.parseInt(strColor[2]));
    	setBackground(col);
    	new Thread(this).start();
    }
    
    /** Initialise the class components */
    private void InitComponents() {
        // try to load the image file
        
    }
 
    /** Calculate the scale required to correctly fit the image into panel */
    private double GetScale(int panelWidth, int panelHeight, int imageWidth, int imageHeight) {
        double scale = 1;
        double xScale;
        double yScale;
        
        if (imageWidth > panelWidth || imageHeight > panelHeight) {
            xScale = (double)imageWidth  / panelWidth;
            yScale = (double)imageHeight / panelHeight;
            scale = Math.min(xScale, yScale);
        }
        else if (imageWidth < panelWidth && imageHeight < panelHeight) {
            xScale = (double)panelWidth / imageWidth;
            yScale = (double)panelHeight / imageHeight;
            scale = Math.min(xScale, yScale);
        }
        else {
            scale = 1;
        }
 
        return scale;
    }
    
    /** Override paint method of the panel */
    public void paint(Graphics g) {
        if( m_Image != null) {
            super.paintComponent(g);
 
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
 
            m_ScaledImage = m_Image;
            
            // Get the required sizes for display and calculations
            int panelWidth = this.getWidth();
            int panelHeight = this.getHeight();
            int imageWidth = m_ScaledImage.getWidth();
            int imageHeight = m_ScaledImage.getHeight();
            
            double scale = GetScale(panelWidth, panelHeight, imageWidth, imageHeight);
            
            /**
            System.out.println("---------------------------------------");
            System.out.println("Panel: "+this.getName()+".");
            System.out.println("Size: "+panelWidth+", "+panelHeight+".");
            System.out.println("Image size: "+m_ScaledImage.getWidth()+", "+m_ScaledImage.getHeight()+".");
            System.out.println("Scale: "+scale+".");
             **/
            
            // Calculate the center position of the panel -- with scale
            double xPos = (panelWidth - scale * imageWidth)/2;
            double yPos = (panelHeight - scale * imageHeight)/2;
 
            // Locate, scale and draw image
            AffineTransform at = AffineTransform.getTranslateInstance(xPos, yPos);
            at.scale(scale, scale);
            g2.drawRenderedImage(m_ScaledImage, at);
        }
    }    
    
    public void stop()
    {
    	start=false;
    }
    public void run(){
    	int countImage=Integer.parseInt(ReadConfig.GET("MEDIA_IMAGE_COUNTER"));
    	String[] imageFiles =ReadConfig.GET("MEDIA_IMAGE_FILES").split("\\|");
    	int sleepCOunter=Integer.parseInt(ReadConfig.GET("MEDIA_IMAGE_SLEEP"));
    	int idx=0;
		  while(start)
		  {
			  String imageFile = "images/" + imageFiles[idx];
			  try {
		            m_Image = ImageIO.read(new File(imageFile));
		            this.repaint();
		            idx++;
		            if(idx==countImage)
		            	idx=0;
		            Thread.sleep(sleepCOunter*1000);
		      } catch (IOException ex) {
		            ex.printStackTrace();
		      }
		      catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
		      }
		  }
    }
}


